﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceBlender.DAL;
using ResourceBlender.Core;
using System.Web.Configuration;
using System.Configuration;

namespace ResourceBlender.NET {
    /// <summary>
    /// Holds configuration data for ResourceBlender.NET.
    /// </summary>
    public class ASPNETConfiguration : IDataConfiguration, IConfiguration {
        /// <summary>
        /// The application version
        /// </summary>
        public static Version Version = new Version(1, 6, 0, 0);
        /// <summary>
        /// The default completion threshold
        /// </summary>
        public const int DefaultThreshold = 40;

        [Flags]
        public enum Permissions : int {
            GenerateResources = 1,
            ManageSelectedResources = 2,
            ManageAllResources = 4,
            ManageApplications = 8,
            ChangeSettings = 16
        }

        private DatabaseType _dbMode;
        private string _dbHost = string.Empty,
                       _dbDatabase = string.Empty,
                       _tablePrefix = string.Empty,
                       _dbUser = string.Empty,
                       _dbPass = string.Empty,
                       _dbPath = string.Empty,
                       _salt = string.Empty;
        private int _dbPort = -1;

        #region IDataConfiguration
        public DatabaseType DBMode {
            get { return _dbMode; }
            set { _dbMode = value; }
        }

        public string DBHost {
            get { return _dbHost; }
            set { _dbHost = value; }
        }

        public int DBPort {
            get { return _dbPort; }
            set { _dbPort = value; }
        }

        public string DBDatabase {
            get { return _dbDatabase; }
            set { _dbDatabase = value; }
        }

        public string TablePrefix {
            get { return _tablePrefix; }
            set { _tablePrefix = value; }
        }

        public string DBUser {
            get { return _dbUser; }
            set { _dbUser = value; }
        }

        public string DBPassword {
            get { return _dbPass; }
            set { _dbPass = value; }
        }

        public string DBPath {
            get { return _dbPath; }
            set { _dbPath = value; }
        }
        #endregion

        #region IConfiguration
        /// <summary>
        /// Gets or sets the salt used to secure passwords.
        /// </summary>
        /// <value>The salt used to secure passwords.</value>
        public string Salt {
            get { return _salt;}
            set { _salt = value; }
        }
        #endregion

        /// <summary>
        /// Initializes a new <see cref="ASPNETConfiguration"/>.
        /// </summary>
        /// <param name="dbMode">The database mode.</param>
        /// <param name="dbHost">The database hostname.</param>
        /// <param name="dbPort">The database port.</param>
        /// <param name="dbDatabase">The name of the database on the server.</param>
        /// <param name="tablePrefix">The table prefix.</param>
        /// <param name="dbUser">The database username.</param>
        /// <param name="dbPass">The database password.</param>
        public ASPNETConfiguration(DatabaseType dbMode, string dbHost, int dbPort, string dbDatabase, string tablePrefix, string dbUser, string dbPass) {
            _dbMode = dbMode;
            _dbHost = dbHost;
            _dbPort = dbPort;
            _dbDatabase = dbDatabase;
            _tablePrefix = tablePrefix;
            _dbUser = dbUser;
            _dbPass = dbPass;
        }

        /// <summary>
        /// Loads configuration data from The Properties.Settings configuration.
        /// </summary>
        public static void LoadConfig() {
            DatabaseType dbType;
            ConnectionStringSettings connectionStringConfig = WebConfigurationManager.ConnectionStrings["Default"];
            switch(connectionStringConfig.ProviderName) {
                case "MySql.Data.MySqlClient":
                    dbType = DatabaseType.MySQL;
                    break;
                case "System.Data.SqlClient":
                default:
                    dbType = DatabaseType.SQLServer;
                    break;
            }

            string dbHost = WebConfigurationManager.AppSettings["dbhost"],
                   dbDatabase = WebConfigurationManager.AppSettings["dbdatabase"],
                   dbUser = WebConfigurationManager.AppSettings["dbuser"],
                   dbPass = WebConfigurationManager.AppSettings["dbpass"],
                   dbPath = WebConfigurationManager.AppSettings["dbpath"],
                   dbTablePrefix = WebConfigurationManager.AppSettings["tableprefix"];
            int dbPort = int.Parse((WebConfigurationManager.AppSettings["dbport"] ?? "-1").ToString());

            var config = new ASPNETConfiguration(dbType, dbHost, dbPort, dbDatabase, dbTablePrefix, dbUser, dbPass);
            DalConfigurationManager.Configure(config);

            config.Salt = WebConfigurationManager.AppSettings["salt"];
            CoreConfigurationManager.Configure(config);
        }

        /// <summary>
        /// Gets the type of the database instance currently in use.
        /// </summary>
        /// <value>The type of the database instance.</value>
        public static DatabaseType DatabaseInstanceType {
            get { return (DatabaseType)int.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["dbtype"]); }
        }
    }
}
